﻿using System;

class SequenceOfGivenSum
{
    static void Main()
    {
        //Exercise 10.
        //Write a program that finds in given array of integers a sequence of given sum S (if present). 
        //Example:	 {4, 3, 1, 4, 2, 5, 8}, S=11 -> {4, 2, 5}

        //enter the length and the elements of the array from the console
        Console.Write("Type the length of the array (N):  ");
        int N = int.Parse(Console.ReadLine());
        Console.WriteLine("Type the elements of the array:");
        int[] array = new int[N];
        for (int i = 0; i < N; i++)
        {
            array[i] = int.Parse(Console.ReadLine());
        }
        //int[] array = { 4, 3, 1, 4, 2, 5, 8 };

        // enter the sum from the console
        Console.Write("Type the sum (S): ");
        int S = int.Parse(Console.ReadLine());         

        int currentSum = 0;       
        int startIndex = -1;
        int lastIndex = -1;
        int counter = 0;

        for (int i = 0; i < array.Length; i++)
        {
            counter = 0;
            currentSum = 0;
            for (int j = i; j < array.Length; j++)
            {
                currentSum += array[j];
                counter++;
                if (currentSum == S)
                {
                    startIndex = i;
                    lastIndex = j;
                    for (int m = startIndex; m <= lastIndex; m++)
                    {
                        Console.Write(array[m] + " ");
                    }
                    Console.WriteLine();
                    return;
                }
                else if (currentSum < S)
                {
                    continue;
                }
                else
                {
                    break;
                }
            }
        }        
        Console.WriteLine("The sum {0} is not present in the array.", S);
    }
}

